package com.bjsxt.usersCenter.common.util;

import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

/**
 * 正则表达式
 * @author wangshSxt
 *
 */
@Component("regexUtil")
public class RegexUtil
{
	public String allStr = "abcdefghijklmnopqrstuvwxyz0123456789";
	private String pattern_$="(\\$\\{)(\\w+)(\\})";
	/**
	 * 生成随机数
	 * @param length	指定长度
	 * @return	返回随机数
	 */
	public String proccedStr(int length)
	{
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for(int i = 0 ; i < length ; i ++)
		{
			int randInt = random.nextInt(allStr.length());
			char ch = allStr.charAt(randInt);
			sb.append(ch);
		}
		return sb.toString() ; 
	}
	
	public String replace$(String resourceStr, Map<String, String> paramsMap)
	{
		
		StringBuffer sb=new StringBuffer();
		Pattern pattern =Pattern.compile(pattern_$);
		Matcher matcher=pattern.matcher(resourceStr);
		
		while(matcher.find())
		{
			String res=matcher.group(2);
			
			if(paramsMap.get(res)!=null)
			{
				matcher.appendReplacement(sb, paramsMap.get(res));
			}
			
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
}
